///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_MAT_DETAIL_CCUMULATIVEOP_H_
#define _BUOLA_MAT_DETAIL_CCUMULATIVEOP_H_

#include <buola/mat/detail/eigen_wrapper.h>
#include <buola/mat/detail/cinverse.h>
#include <buola/mat/cmat.h>

namespace buola { namespace mat { namespace detail {

DECLARE_NAME(CumulativeRows)
DECLARE_NAME(CumulativeCols)
DECLARE_NAME(CumulativeAll)
    
template<typename tSub,typename tOp,typename tNegOp,typename tMode>
struct MMatrixTraits<CCumulativeOp<tSub,tOp,tNegOp,tMode>>
{
private:
    typedef MMatrixTraits<ODecay<tSub>> TSubTraits;
    
public:
    typedef typename TSubTraits::TScalar TScalar;
    static const size_t cRows=TSubTraits::cRows;
    static const size_t cCols=TSubTraits::cCols;
    static const bool cRowLinearAccess=false;
    static const bool cColLinearAccess=false;
    static const bool cRowPacketAccess=false;
    static const bool cColPacketAccess=false;
    static const bool cMightAlias=TSubTraits::cMightAlias;
    static const int cElementAccess=ACCESS_NO;
    static const bool cEigenMappable=false;
    static const bool cKeepConstness=false;
    static const bool cExprAssignable=false;
    static const bool cIsMatrix=false;
};

//totally fixed size version
template<typename tSub,typename tOp,typename tNegOp,typename tMode>
class CCumulativeOp : public CExpr<CCumulativeOp<tSub,tOp,tNegOp,tMode>>
{
    typedef MMatrixTraits<CCumulativeOp<tSub,tOp,tNegOp,tMode>> TSelfTraits;

public:
    CCumulativeOp(tSub pSub,const tOp &pOp,const tNegOp &pNegOp)
        :   mSub(pSub)
        ,   mOp(pOp)
        ,   mNegOp(pNegOp)
    {}

    CCumulativeOp(const CCumulativeOp&)=delete;
    CCumulativeOp& operator=(const CCumulativeOp&)=delete;

public:    
    size_t Rows() const  {   return mSub.Rows();  }
    size_t Cols() const  {   return mSub.Cols();  }

private:
    template<typename tMat>
    void DoEvalTo(tMat &pDst,NCumulativeAll) const
    {
        const auto &lSub=element_accessible<ACCESS_ONCE>(mSub);
        
        int lRows=mSub.Rows();
        int lCols=mSub.Cols();
        
        if(lRows==0||lCols==0) return;
        
        pDst(0,0)=lSub(0,0);
        
        for(int c=1;c<lCols;c++)
            pDst(0,c)=mOp(pDst(0,c-1),lSub(0,c));
        
        for(int r=1;r<lRows;r++)
        {
            pDst(r,0)=mOp(pDst(r-1,0),lSub(r,0));
            for(int c=1;c<lCols;c++)
                pDst(r,c)=mNegOp(mOp(pDst(r-1,c),mOp(pDst(r,c-1),lSub(r,c))),pDst(r-1,c-1));
        }
    }
    
    template<typename tMat>
    void DoEvalTo(tMat &pDst,NCumulativeRows) const
    {
        const auto &lSub=element_accessible<ACCESS_ONCE>(mSub);
        
        int lRows=mSub.Rows();
        int lCols=mSub.Cols();
        
        if(lRows==0||lCols==0) return;
        
        for(int r=0;r<lRows;r++)
        {
            pDst(r,0)=lSub(r,0);
        
            for(int c=1;c<lCols;c++)
                pDst(r,c)=mOp(pDst(r,c-1),lSub(r,c));
        }
    }
    
    template<typename tMat>
    void DoEvalTo(tMat &pDst,NCumulativeCols) const
    {
        const auto &lSub=element_accessible<ACCESS_ONCE>(mSub);
        
        int lRows=mSub.Rows();
        int lCols=mSub.Cols();
        
        if(lRows==0||lCols==0) return;
        
        for(int c=0;c<lCols;c++)
            pDst(0,c)=lSub(0,c);

        for(int r=1;r<lRows;r++)
        {
            for(int c=0;c<lCols;c++)
                pDst(r,c)=mOp(pDst(r-1,c),lSub(r,c));
        }
    }
    
public:
    template<typename tMat>
    void EvalTo(tMat &pDst) const
    {
        DoEvalTo(pDst,tMode());
    }

private:
    tSub mSub;
    tOp mOp;
    tNegOp mNegOp;
};

/*namespace detail*/ } /*namespace mat*/ } /*namespace buola*/ }

#endif
